package com.Client;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;

import com.Serverce.Config;
import com.Util.GeneralThreadPool;
import com.Util.StreamUtil;
import com.echo.LanSecurityManager;


public class ClientThread implements Runnable {
	private Socket _socket;
	private Socket _serverSocket;
	private InputStream _in;
	private OutputStream _out;
	private InputStream _serverIn;
	private OutputStream _serverOut;
	private String _ip;
	public boolean _isOK;
	public long loginTime = System.currentTimeMillis();
	private int _port = 0;

	public ClientThread(Socket socket , final int port) throws IOException{
		_socket = socket;
		_in = _socket.getInputStream();
		_out =_socket.getOutputStream();
		_serverSocket = new Socket();
		_ip = _socket.getInetAddress().getHostAddress();
		_port = port;
	}

	@Override
	public void run() {
		LanSecurityManager.BANIPPACK.put(this._ip, 300);
		try{
			InetSocketAddress isa = new InetSocketAddress(Config.TARGET_SERVER_IP, _port);
			_serverSocket.connect(isa);
			if(_serverSocket.isConnected()){
				System.out.println("連線成功: "+_ip+" : "+_port);
			}
			_serverIn = _serverSocket.getInputStream();
			_serverOut = _serverSocket.getOutputStream();
			try {
				BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(_serverOut));
				bfw.write(_ip);
				bfw.newLine();
				bfw.flush();
			} catch (Exception e1) {
				System.out.println(_ip+" : "+_port+" 連線錯誤。");
				e1.printStackTrace();
				return;
			}
//			int count =0;
//			while(true){
//				if(count >3000){
//					break;
//				}
//				BufferedReader br = new BufferedReader(new InputStreamReader(_serverIn)); 
//				String line =br.readLine();
//				if(line !=null){
//					System.out.println(line);
//					if(line.equalsIgnoreCase("OK")){
//						try {
//							BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(_serverOut));
//							bfw.write(_ip);
//							bfw.newLine();
//							bfw.flush();
//						} catch (Exception e1) {
//							e1.printStackTrace();
//						}
//					}
//					break;
//				}
//				count++;
//				Thread.sleep(1);
//			}
			GeneralThreadPool.getInstance().execute(new HcPack());
			while(_socket != null){
				read();
				Thread.sleep(1);
			}
		} catch (final Exception e) {
//			e.printStackTrace();
		} finally {
			close();
		}
	}


	public void close(){
		Config.CLIENT_THREAD.remove(this);
		if(_socket == null && _serverSocket == null){
			return;
		}
		StreamUtil.close(_socket , _in , _out);
		StreamUtil.close(_serverSocket , _serverIn , _serverOut);
		_socket=null;
		_serverSocket =null;
		_serverIn = null;
		_serverOut = null;
		_in = null;
		_out = null;
		System.out.println("斷線: "+_ip+" : "+_port);
	}

	private void read() throws IOException{
		byte[] data = new byte[1440];
		int length;
		while (_in !=null && (length = _in.read(data)) != -1) {
			if(_serverOut !=null){
				if(!_isOK){
					_isOK = true;
					LanSecurityManager.BANIPPACK.remove(_ip);
				}
				_serverOut.write(data, 0, length);
			}else{
				break;
			}
		}
	}

	private void write() throws IOException{
		byte[] data = new byte[1440];
		int length;
		while (_serverIn !=null && (length = _serverIn.read(data)) != -1) {
			if(_out !=null){
				_out.write(data, 0, length);
			}else{
				break;
			}
		}
	}
	public String getIP(){
		return _ip;
	}

	class HcPack implements Runnable {

		@Override
		public void run() {
			try {
				while(_socket != null){
					write();
					Thread.sleep(1);
				}
			} catch (Exception e) {
				close();
			}
		}
	}
}
